<?php
/**
 * 标签功能实现
 *
 * User: kaikaic
 * Date: 2018/4/20
 * Time: 12:16
 */

namespace app\admin\service;

use app\admin\model\CategoryModel;
use service\BasicService;
use think\Db;

class TagsService extends BasicService {

    protected function init() {
        $this->dataTable = ['SystemTags'];
    }

    /**
     * 根据分类简写获取关联标签
     */
    public function listTagsByCategory($abbr) {
        $category = CategoryModel::getCategoryByAbbr($abbr);

        return Db::name($this->dataTable[0])->where('category_id', $category['id'])->select();
    }

    /**
     * KV格式标签数组
     */
    public function getTagsArrayByAbbr($abbr) {
        $category = CategoryModel::getCategoryByAbbr($abbr);
        $records = Db::name($this->dataTable[0])->where('category_id', $category['id'])->select();

        $tagArray = array();
        for ($i=0; $i<count($records); $i++) {
            $temp = $records[$i];

            $tagArray[$temp['id']] = $temp['name'];
        }

        return $tagArray;
    }

    /**
     * 根据tagID数组读取tag信息
     * @return key=>value数组
     */
    public function getTags($tagIDs) {
        $tagList = array();

        if (!is_array($tagIDs)) {
            return $tagList;
        }

        $recordList = Db::name($this->dataTable[0])->where('id', 'in', $tagIDs)->select();
        for ($i=0; $i<count($recordList); $i++) {
            $temp = $recordList[$i];

            $tagList[$temp['id']] = $temp['name'];
        }

        return $tagList;
    }
}

